﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Dal.Employee;//数据
using Gx.Model.Entity;//实体
using Gx.Common;//通用类
using Gx.Model;
using Gx.Model.Enums;
using Gx.Bll.Shared;
using System.Linq;

namespace Gx.Bll.Employee
{
    /// <summary>
    /// 库存警戒线
    /// </summary>
    public class G_StocksCordonInfoBll
    {
        public ReturnModel<bool> AddG_StocksCordonInfo(G_StocksCordonInfo model)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                model.AddTime = model.LastEditTime = DateTime.Now;
                model.AddUser = model.LastEditUser = LoginHelper.GetUserId();
                int GoodsCount = ExiststG_StocksCordonInfo(model.GoodsId);
                if (GoodsCount > 0)
                {
                    r.Code = (int)Code.重复数据;
                    r.Msg = "请勿重复添加该物料!";
                    return r;
                }
                bool res = dal.AddG_StocksCordonInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("AddG_StocksCordonInfo", "添加一条库存警戒线信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                    return r;
                }
                return r;
            }
        }

        public ReturnModel<bool> EditG_StocksCordonInfo(G_StocksCordonInfo model)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                model.LastEditTime = DateTime.Now;
                model.LastEditUser = LoginHelper.GetUserId();
                int GoodsCount = ExiststG_StocksCordonInfo(model.GoodsId);
                if (GoodsCount > 1)
                {
                    r.Code = (int)Code.重复数据;
                    r.Msg = "请勿重复添加该物料";
                    return r;
                }
                bool res = dal.EditG_StocksCordonInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("EditG_StocksCordonInfo", "编辑一条库存警戒线信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                    return r;
                }
                return r;
            }
        }

        public ReturnModel<bool> DelG_StocksCordonInfo(int StocksCordonId)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                G_StocksCordonInfo sc = GetG_StocksCordonInfoModel(StocksCordonId);
                bool res = dal.DelG_StocksCordonInfo(StocksCordonId);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("DelG_StocksCordonInfo", "删除一条库存警戒线信息", sc);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                    return r;
                }
                return r;
            }
        }


        private List<G_StocksCordonInfo> GetG_StocksCordonInfoList(string strWhere)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                return dal.GetG_StocksCordonInfoList(strWhere);
            }
        }


        public List<G_StocksCordonInfo> GetG_StocksCordonInfoList(string GoodsName,
        int pageIndex, int pageSize, out int totalCount)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                if (!GoodsName.IsNullOrEmpty())
                {
                    strWhere.AppendFormat(" AND (GoodsName LIKE'%{0}%' OR GoodsCode LIKE '%{0}%' OR GoodsFull LIKE'%{0}%' OR GoodsJianPin LIKE'%{0}%')", GoodsName);
                }
                return dal.GetG_StocksCordonInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
            }
        }


        public int ExiststG_StocksCordonInfo(int GoodsId)
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat(" AND dbo.G_StocksCordonInfo.GoodsId={0}", GoodsId);
            return GetStocksCordonInfoCount(strWhere.ToString());
        }

        private int GetG_StocksCordonInfoCount(string strWhere)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                return dal.GetG_StocksCordonInfoCount(strWhere);
            }
        }
        private int GetStocksCordonInfoCount(string strWhere)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                return dal.GetG_StocksCordonInfoCount(strWhere);
            }
        }




        public G_StocksCordonInfo GetG_StocksCordonInfoModel(int stocksTotalId)
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                G_StocksCordonInfo model = dal.GetG_StocksCordonInfoModel(stocksTotalId);
                return model;
            }
        }

        public List<G_StocksCordonInfo> GetG_StocksCordonInfoList()
        {
            using (G_StocksCordonInfoDal dal = new G_StocksCordonInfoDal())
            {
                List<G_StocksCordonInfo> tcList = dal.GetG_StocksCordonInfoList();
                if (tcList.Count == 0)
                {
                    tcList.Add(new G_StocksCordonInfo() { GoodsName = "库存正常" });
                    return tcList;
                }
                List<int> goodsIdList = tcList.Select(x => x.GoodsId).Distinct().ToList();
                List<G_GoodsInfo> goodsList = new G_GoodsInfoBll().GetG_GoodsInfoList(goodsIdList);
                foreach (var item in tcList)
                {
                    G_GoodsInfo goods = goodsList.Find(x => x.GoodsId == item.GoodsId);
                    if (goods == null)
                    {
                        item.GoodsName = "未找到该商品或已被删除";
                        continue;
                    }
                    item.GoodsName = goods.GoodsName;
                    item.ColorName = goods.ColorName;
                    item.TypeName = goods.TypeName;
                    item.Specs = goods.Specs;
                    item.StrNum = item.Num.ToStringf2();
                }

                return tcList;
            }
        }
    }
}
